﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using StoragePeriod.BLL.FoodTesting;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.Web.Common;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.RawDict;


namespace StoragePeriod.Web.FoodTesting.CuFeZnFood
{
    public partial class CuFeZnEdit : System.Web.UI.Page
    {
        T_FoodRecord tFoodRecord = new T_FoodRecord();  //检测记录
        T_CuFeZnData tCuFeZnData = new T_CuFeZnData();              //Vc检测记录
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作
        public string FoodName = "铜";

        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Request.QueryString["MineralTypeID"].ToString())
            {
                case "10":
                    this.MineralTypeID.Value = "10";
                    this.FoodName = "铜";
                    break;
                case "11":
                    this.MineralTypeID.Value = "11";
                    this.FoodName = "铁";
                    break;
                case "12":
                    this.MineralTypeID.Value = "12";
                    this.FoodName = "锌";
                    break;
            }

            if (!IsPostBack)
            {
                if (Request.QueryString["RawID"] != null && Request.QueryString["FoodTime"] != null && Request.QueryString["FoodNum"] != null)
                {
                    this.LoadData();
                }
            }
        }

        //加载数据
        public void LoadData()
        {
            
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());

            List<E_CuFeZnData> eCuFeZnDataList = tCuFeZnData.GetModelList("RawID=" + RawId + " and FoodTime=cast('" + date.ToString() + "' as datetime) and MineralTypeID=" + Request.QueryString["MineralTypeID"].ToString());

            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = int.Parse(Request.QueryString["MineralTypeID"].ToString());//膳食纤维检测
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eFoodRecord != null)
            {
                this.labMean.Text = eFoodRecord.FoodResult;
            }

            if (eCuFeZnDataList != null)
            {
                int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
                E_CuFeZnData eCuFeZnData = eCuFeZnDataList.Where(p => p.FoodNum == FoodNum).FirstOrDefault();//获取第一次检测记录
                if (eCuFeZnData != null)
                {
                    this.txt_P1.Text = eCuFeZnData.P1;
                    this.txt_P0.Text = eCuFeZnData.P0;
                    this.txt_V1.Text = eCuFeZnData.V1;
                    this.txt_M.Text = eCuFeZnData.M;

                    this.txt_W.Text = eCuFeZnData.Result;
                }
            }
        }

        //提交数据
        protected void btPost_Click(object sender, EventArgs e)
        {
            int RawId = Convert.ToInt32(Request.QueryString["RawID"]);
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"]);
            int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
            try
            {
                double P1 = Convert.ToDouble(this.txt_P1.Text.Trim());
                double P0 = Convert.ToDouble(this.txt_P0.Text.Trim());
                double V1 = Convert.ToDouble(this.txt_V1.Text.Trim());
                double M = Convert.ToDouble(this.txt_M.Text.Trim());

                double Result = (P1-P0)*V1/M;
                Convert.ToInt32(Result);

                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);

                E_CuFeZnData eCuFeZnData = new E_CuFeZnData();
                eCuFeZnData.RawID = RawId;
                eCuFeZnData.RawName = eRawMaterial.RAWName;
                eCuFeZnData.P1 = P1.ToString();
                eCuFeZnData.P0 = P0.ToString();
                eCuFeZnData.V1 = V1.ToString();
                eCuFeZnData.M = M.ToString();
                eCuFeZnData.Result = Result.ToString("f2");
                eCuFeZnData.FoodTime = date;
                eCuFeZnData.FoodNum = FoodNum;
                eCuFeZnData.MineralTypeID = int.Parse(Request.QueryString["MineralTypeID"].ToString());//膳食纤维检测
                tCuFeZnData.RemoveByFoodNum(eCuFeZnData);   //删除现有的检测记录
                tCuFeZnData.Add(eCuFeZnData);               //添加检测记录
                this.txt_W.Text = eCuFeZnData.Result;
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('提交成功！');</script>");
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('输入有误！');</script>");
            }
        }

        //计算平均值
        protected void btnMean_Click(object sender, EventArgs e)
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());

            List<E_CuFeZnData> eCuFeZnDataList = tCuFeZnData.GetModelList("RawID=" + RawId + " and FoodTime=cast('" + date.ToString() + "' as datetime) and MineralTypeID=" + Request.QueryString["MineralTypeID"].ToString());


            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = int.Parse(Request.QueryString["MineralTypeID"].ToString()); ;//铜检测
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eCuFeZnDataList == null || eCuFeZnDataList.Count < 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('请先提交检测数据！');</script>");
                return;
            }

            double Result = 0;//检测结果
            eCuFeZnDataList.ForEach(p =>
            {
                Result += Convert.ToDouble(p.Result);
            });

            if (eFoodRecord != null)  //若存在则进行修改
            {
                eFoodRecord.FoodResult = (Result / eCuFeZnDataList.Count).ToString("f2");
                tFoodRecord.Update(eFoodRecord);
                this.labMean.Text = eFoodRecord.FoodResult;
            }
            else
            {
                md_FoodRecord.FoodResult = (Result / eCuFeZnDataList.Count).ToString("f2");
                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);
                md_FoodRecord.RawName = eRawMaterial.RAWName;
                tFoodRecord.Add(md_FoodRecord);
                this.labMean.Text = md_FoodRecord.FoodResult;
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('计算完毕！');parent.window.RefValue();</script>");
        }
    }
}